package com.hibase.core.log.util;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.hibase.common.util.UserContext;
import com.hibase.core.log.entity.MdsOperationLog;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 系统日志工具类
 *
 * @author L.cm
 */
@Slf4j
@UtilityClass
public class HibaseLogUtil {

    public static MdsOperationLog getOperationLog() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects
                .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        MdsOperationLog operationLog = new MdsOperationLog();
        operationLog.setCreateBy(UserContext.get().getLoginName());
        operationLog.setUrl(URLUtil.getPath(request.getRequestURI()));
        operationLog.setRequestJson(HttpUtil.toParams(request.getParameterMap()));
        operationLog.setOperator(UserContext.get().getLoginName());
        return operationLog;
    }

    public static MdsOperationLog getOperationLog(String type,String source,String msg){
        MdsOperationLog log = HibaseLogUtil.getOperationLog();
        log.setType(type);
        log.setSource(source);
        log.setRequestJson(msg);
        return log;
    }
}
